//
//  FSSecondCellPhoneController.m
//  WZSOA
//
//  Created by 刘沿 on 16/7/18.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSSecondCellPhoneController.h"

@interface FSSecondCellPhoneController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation FSSecondCellPhoneController

static NSString *const reuserId = @"SecondCell";

-(NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }

    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加一个头部的刷新控件
    __weak typeof (self)WeakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [WeakSelf loadData];
        
        
    }];
    
    //主动让下拉刷新头部控件进入刷新状态
    [self.tableView headerBeginRefreshing];
}

- (void)loadData{

    NSString *url = nil;
    
    NSString *method = nil;
    
    if (self.isThree.length != 0) {
        
        url = Kexchother;
        
        method = kBmTxlTree;
        
    }else {
    
        url = Kexchother;
    
        method = KBmRyTxlTree;
    }

    NSArray *array = @[@{@"unid":self.unid}];
    
    [HttpTools requestWithURL:url arrays:array method:method success:^(id JSON) {
        
        if (self.isThree.length != 0) {
            
            NSArray *tempArray = JSON[@"data"];
            
            NSDictionary *dict1 = tempArray.firstObject;
            
            for (NSDictionary *dict in dict1[@"Parent"]) {
                
                FSSecondModel *model = [[FSSecondModel alloc] init];

                model.name = dict[@"name"];
                model.unid = dict[@"unid"];
                
                [self.dataArray addObject:model];
                
            }
            [self.tableView headerEndRefreshing];
            [self.tableView reloadData];
            
        }else {
            NSArray *tempArray = JSON[@"data"];
            
            NSDictionary *dict = tempArray.firstObject;
            
            for (NSDictionary *dict1 in dict[@"Parent"]) {
                
                FSSecondModel *model = [[FSSecondModel alloc] init];
                
                model.duties = dict1[@"duties"];
                model.surname = dict1[@"surname"];
                model.name = dict1[@"name"];
                model.unid = dict1[@"unid"];
                
                [self.dataArray addObject:model];
                
            }
            [self.tableView headerEndRefreshing];
            [self.tableView reloadData];

        }
        
    
    } failure:^(NSError *error) {
        
    }];
    
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.isThree.length != 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserId];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserId];
        }
        
        FSSecondModel *model = self.dataArray[indexPath.row];
        
        cell.textLabel.text = model.name;
        
        return cell;
        
    }else {
    
        FSSecondModel *model = self.dataArray[indexPath.row];
        
        FSThirdPhoneCell *cell = [FSThirdPhoneCell ThirdPhoneCell];
        
        cell.model = model;
        
        return cell;
    
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FSSecondModel *model = self.dataArray[indexPath.row];
    if (self.isThree.length != 0) {
        
        FSSecondCellPhoneController *decVC = [[FSSecondCellPhoneController alloc] init];
        
        decVC.unid = model.unid;
        
        decVC.isThree = @"";
        
        [self.navigationController pushViewController:decVC animated:YES];
        
    }else {
//详情页面

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        FSThirdCellPhoneController *thirdVC = [story instantiateViewControllerWithIdentifier:@"ThirdCellVC"];
        
        thirdVC.unid = model.unid;
        thirdVC.name = model.name;
        
        [self.navigationController pushViewController:thirdVC animated:YES];
    }
    

}




@end
















